package handlers

import (
	"net/http"
	"strconv"

	"gitee.com/firewing_group/model/comm"

	"gitee.com/firewing_group/test/logic"
	"gitee.com/firewing_group/test/someclient"

	"gitee.com/firewing_group/configenv"

	"gitee.com/firewing_group/model/ctxdata"
	"gitee.com/firewing_group/model/model/stuser"
	"github.com/labstack/echo"
)

func GetAideList(c echo.Context) error {
	cc := c.Get("cc").(*ctxdata.Cusctx)
	user := ctxdata.GetUser(c).(*stuser.KuaiMaoUser)
	rewarduplimit, rewarddownlimit, adNumberLimt, adNumber, timeboom, whatboom, _ := GetValueFromConfig()
	ad, err := logic.GetAide(cc, user, adNumberLimt, adNumber, rewarduplimit, rewarddownlimit, timeboom, whatboom)
	if err != nil {
		return c.JSON(http.StatusBadRequest, comm.Bad("GetAideList_Wrong", comm.STATUS_BAD_REQUEST))
	}
	return c.JSON(http.StatusOK, ad)
}

// 如果已经看了，则直接返回相应结果
func KanAide(c echo.Context) error {
	cc := c.Get("cc").(*ctxdata.Cusctx)
	user := ctxdata.GetUser(c).(*stuser.KuaiMaoUser)
	indexString := c.Param("index")
	index, _ := strconv.Atoi(indexString)
	if index > 2 {
		err := "传入广告错误"
		cc.Errf(err)
		return c.JSON(http.StatusBadRequest, comm.Bad("Wrong_Index", comm.STATUS_BAD_REQUEST))
	}
	lastAD := logic.LookedAide(cc, user, indexString)
	if lastAD == nil {
		return c.JSON(http.StatusBadRequest, comm.Bad("KanAide_Wrong", comm.STATUS_BAD_REQUEST))
	}
	lastAD.Inform[indexString].Base = 100
	return c.JSON(http.StatusOK, lastAD.Inform[indexString])
}

// 点击广告，该广告所有的奖励乘以 2.4，包括算力和氪晶，算力的持续时间不变
// 如果没看则先触发 KanAide 的逻辑，再触发本逻辑
// todolist
func ClickAide(c echo.Context) error {
	cc := c.Get("cc").(*ctxdata.Cusctx)
	user := ctxdata.GetUser(c).(*stuser.KuaiMaoUser)
	indexString := c.Param("index")
	index, _ := strconv.Atoi(indexString)
	if index > 2 {
		err := "传入广告错误"
		cc.Errf(err)
		return c.JSON(http.StatusBadRequest, comm.Bad("Wrong_Index", comm.STATUS_BAD_REQUEST))
	}

	lastAD := logic.ClickAide(cc, user, indexString)
	if lastAD == nil {
		return c.JSON(http.StatusBadRequest, comm.Bad("ClickAide_wrong", comm.STATUS_BAD_REQUEST))
	}
	lastAD.Inform[indexString].Base = 100
	return c.JSON(http.StatusOK, lastAD.Inform[indexString])
}
func GetAideReward(c echo.Context) error {
	cc := c.Get("cc").(*ctxdata.Cusctx)
	animalcc := ctxdata.New(cc.C, someclient.GetAnimalDB(), cc.B)
	user := ctxdata.GetUser(c).(*stuser.KuaiMaoUser)
	indexString := c.Param("index")
	index, _ := strconv.Atoi(indexString)
	if index > 2 {
		err := "传入广告错误"
		cc.Errf(err)
		return c.JSON(http.StatusBadRequest, comm.Bad("Wrong_Index", comm.STATUS_BAD_REQUEST))
	}
	_, _, _, _, _, _, validity := GetValueFromConfig()
	err := logic.GetAideReward(cc, animalcc, user, indexString, validity)
	if err != nil {
		return c.JSON(http.StatusBadRequest, comm.Bad("GetAideReward_Wrong", comm.STATUS_BAD_REQUEST))
	}
	return c.JSON(http.StatusOK, "OK")
}

func GetIntFromConfig(_config string, _value int) int {
	a := configenv.GetEnvAsIntWithDefault(_config, 0)
	if a == 0 {
		return _value
	} else {
		return a
	}
}

func GetValueFromConfig() (int, int, int, int, int, int, int) {
	//每天多少个广告
	adNumberLimt := GetIntFromConfig("adlimit", 20)
	//多少个广告一组
	adNumber := GetIntFromConfig("adnumber", 2)
	//第多少个广告翻倍
	timeboom := GetIntFromConfig("timeboom", 5)
	//翻多少倍
	whatboom := GetIntFromConfig("whatboom", 5)
	//奖励keth最大值
	rewarduplimit := GetIntFromConfig("rewarduplimit", 5)
	//奖励keth最小值
	rewarddownlimit := GetIntFromConfig("rewarddownlimit", 2)
	//临时算力持续多少天
	validity := GetIntFromConfig("validity", 3)
	return rewarduplimit, rewarddownlimit, adNumberLimt, adNumber, timeboom, whatboom, validity
}
